package com.hsyt.pms.controller.org;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.org.OrgDto;
import com.hsyt.pms.api.dto.org.OrgTypeQueryDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.enums.OrgType;
import com.hsyt.pms.api.enums.YesOrNo;
import com.hsyt.pms.api.vo.org.OrgHelp;
import com.hsyt.pms.api.vo.org.OrgTypeVo;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.org.dubbo.OrgService;
import com.hsyt.pms.org.dubbo.OrgTypeService;
import com.hsyt.pms.sys.dubbo.SysCityService;
import com.hsyt.pms.sys.dubbo.SysDictionaryService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.Map;

/**
 * @ClassName: OrgTypeController
 * @Description: 组织控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping("/pms/org/type")
public class OrgTypeController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrgTypeController.class);

    @Reference(group = "pmsOrg", registry = "pms", version = "0.0.1")
    private OrgService orgService;

    @Reference(group = "pmsOrg", registry = "pms", version = "0.0.1")
    private OrgTypeService orgTypeService;

    @Reference(group = "pmsOrg", registry = "pms", version = "0.0.1")
    private SysCityService sysCityService;

    @Reference(group = "pmsOrg", registry = "pms", version = "0.0.1")
    private SysDictionaryService sysDictionaryService;

    @RequestMapping(value = "/page")
    public ModelAndView page(ModelMap model, Integer orgType) {
        String orgTypeComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(OrgType.class, "value", "name");
        model.put("orgTypeComboData", orgTypeComboData);
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        //初始化枚举类
        String isOrNotComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(YesOrNo.class);
        model.put("isOrNotComboData", isOrNotComboData);
        model.put("orgType", orgType);
        return new ModelAndView("org/type/pageOrgType", model);
    }

    @RequestMapping(value = "/findPageData")
    public @ResponseBody
    PageResult findPageData(Integer orgType, @RequestBody OrgTypeQueryDto dto) {
        PageResult<OrgTypeVo> pageResult = new PageResult<OrgTypeVo>();
        try {
            pageResult = orgService.searchPageDataByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("BuildingController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }

    @RequestMapping(value = "/editPage")
    public ModelAndView editPage(ModelMap model, @PathVariable String orgType, String type, Long parentOrgId) {
        model.put("type", type);
        model.put("orgType", orgType);
        String orgTypeComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(OrgType.class, "value", "name");
        model.put("orgTypeComboData", orgTypeComboData);
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        if (StringUtils.equals("add", type)) {
            Integer oType = OrgType.getOrgTypeValue(orgType);
            OrgDto initDto = new  OrgDto();
            initDto.setOrgType(oType);
            model.put("initDto", initDto);
        }
        return new ModelAndView("org/win/editOrg" + OrgType.getOrgTypePageValue(orgType), model);
    }

    @RequestMapping(value = "/toEditForm")
    public ModelAndView toEditForm(ModelMap model, @RequestParam("formType") String formType, @RequestParam("orgType") Long orgType) {
        model.put("formType", formType);
        model.put("orgType", orgType == null ? 1L : orgType);
        return new ModelAndView("org/type/formOrgType");
    }

    @RequestMapping(value = "/doUndo")
    public @ResponseBody
    ResultMessage doUndo(@RequestBody OrgDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "操作成功");
        try {
            this.orgService.updateByPrimaryKey(dto);
        } catch (Exception e) {
            result.change(false, "操作失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "OrgTypeController-doUndo error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    @RequestMapping(value = "/save")
    public @ResponseBody
    ResultMessage save(@RequestParam Map<String, Object> model, String formType, Long pid, Integer orgType,
                       HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            OrgDto dto = OrgHelp.createOrgDto(orgType);
            System.out.println("map->" + model);
            if (dto.getId() == null) {
                if (org.apache.commons.lang3.StringUtils.isBlank(dto.getStatus())) {
                    dto.setStatus(AbleStatus.ENABLED.getCode());
                }
                //初始化上级及系统编码
                String nextSysOrgCode = orgService.nextSysOrgCode(pid, orgType);
                dto.setParentOrgCode(OrgHelp.makeParentSysOrgCode(nextSysOrgCode));
                dto.setSysOrgCode(nextSysOrgCode);
                dto.setOrgLevel(OrgHelp.makeOrgLevel(nextSysOrgCode));
                orgTypeService.insertOrg(dto);
            } else {
                orgTypeService.updateByPrimaryKey(dto);
            }
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysParamController-save error: {0}", e.getMessage());
        }
        return result;
    }
}
